import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../components/login.vue'
import home from '../components/home.vue'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/', redirect: '/login' },
    { path: '/login', component: login },
    { path: '/home', component: home }
  ]
})
router.beforeEach((to, from, next) => { // 导航守卫
  if (to.path === '/login') return next() // 访问登录直接放行
  if (!sessionStorage.getItem('token')) return next('/login') // 判断是否有token
  next()
})

export default router